

package com.beans;


import com.entidades.Negocios;
import com.entidades.Ofertas;
import com.entidades.Usuarios;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.el.ELException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.Transactional;
import javax.transaction.UserTransaction;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.primefaces.event.FileUploadEvent;  
import org.primefaces.event.SelectEvent;
import org.primefaces.event.SlideEndEvent;
import org.primefaces.event.map.PointSelectEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;  
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;

    
/**
 * <p>A simple managed bean to mediate between the user
 * and the persistence layer.</p>
 * @author rlubke
 */
@Named("cBusOfe")
public class controlBuscarOfertas {
    /**
     * <p>The <code>PersistenceContext</code>.</p>
     */
    @PersistenceContext 
    private EntityManager em;
    
    /**
     * <p>The transaction resource.</p>
     */
    @Resource 
    private UserTransaction utx2;

    
    public controlBuscarOfertas() {  
    }  

    //variablesm buscar ofertas/cupones
    public List<Ofertas> vListaCupones;
    public Ofertas selectedOfe;
    public String orderBy;
    public Double minPrecio;
    public Double maxPrecio;
    public String tipoOferta;
    public String NegocioOferta;

    public String getNegocioOferta() {
        return NegocioOferta;
    }

    public void setNegocioOferta(String NegocioOferta) {
        this.NegocioOferta = NegocioOferta;
    }

    
    public Ofertas getSelectedOfe() {
        return selectedOfe;
    }

    public void setSelectedOfe(Ofertas selectedOfe) {
        
        Map sMap;
        sMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        controlNegocios cn = (controlNegocios) sMap.get("cNeg");
        
        cn.negocioOferta = cn.findNegocioPorId(selectedOfe.getOfeNegocio());
        
        this.selectedOfe = selectedOfe;
    }

    public String getOrderBy() {
        if(orderBy == null){orderBy = "id_oferta";}
        return orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public Double getMinPrecio() {
        if(minPrecio == null){ minPrecio = 0.00;}
        System.out.println("GET min precio--> " + minPrecio);
        return minPrecio;
    }

    public void setMinPrecio(Double minPrecio) {
        this.minPrecio = minPrecio;
    }

    public Double getMaxPrecio() {
        if(maxPrecio == null){ maxPrecio = 1000.00;}
        System.out.println("GET max precio--> " + maxPrecio);
        return maxPrecio;
    }

    public void setMaxPrecio(Double maxPrecio) {
        this.maxPrecio = maxPrecio;
    }

    public String getTipoOferta() {
        if(tipoOferta == null){tipoOferta = "";}
        return tipoOferta;
    }

    public void setTipoOferta(String tipoOferta) {
        this.tipoOferta = tipoOferta;
    }

    public List<Ofertas> getvListaCupones() {
        vListaCupones = findOfertas(orderBy, maxPrecio, minPrecio, tipoOferta);
        //vListaCupones = findOfertas(null, 1.00, 1.00, "");
        return vListaCupones;
    }

    public void setvListaCupones(List<Ofertas> vListaCupones) {
        this.vListaCupones = vListaCupones;
    }

    
  

        public List<Ofertas> findOfertas(String orderBy, Double maxPrecio, Double minPrecio, String tipo) {
            
            
            
            
            if(orderBy == null || orderBy.isEmpty()){
                orderBy = "id_oferta";
                maxPrecio = 10000.00;
                minPrecio = 0.00;
                tipo = "";
            }
            
            String SQLstring;
            SQLstring = "select * from bdagrupados.ofertas "
                       +" where ofe_tipo like '%" + tipo + "%'"
                       +" and ofe_fecfin >= CURDATE()"
                       +" order by " + orderBy;
            System.out.println("SQL ejecutada: " + SQLstring);
            Query query = em.createNativeQuery(SQLstring, Ofertas.class);
            //Query query = em.createNamedQuery("Ofertas.findAll");
            List<Ofertas> tmpOfertas = query.getResultList();
            return tmpOfertas;
        }
        
        public String DetalleOferta(Integer oferId) {
            
            Query query = em.createNamedQuery("Ofertas.findByIdOferta");
            query.setParameter("idOferta", oferId);
            Ofertas tO = (Ofertas)query.getSingleResult();
            
            String cadValidez;
            String cadDetalle;
            if(tO.getOfePeriodovalidez().equals(0)){
                cadValidez = "Oferta valida para disfrutarse hasta la fecha: " + tO.getOfeFecfin();
            }else{
                cadValidez = "Oferta valida para disfrutarse hasta " + tO.getOfePeriodovalidez() + " dias desde la fecha de compra.";
            }
            
            cadDetalle = "<p class='MsoNormal' style='text-align:center' align='center'><b style='mso-bidi-font-weight:\n" +
                "normal'><span style='font-size:16.0pt;line-height:115%;font-family:&quot;Arial Black&quot;,&quot;sans-serif&quot;;\n" +
                "mso-bidi-font-family:Arial;mso-fareast-language:ES;mso-no-proof:yes'>" + tO.getOfeTitulo() + "</span></b></p>\n" +
                "    <p class='MsoNormal' style='text-align:center' align='center'><span style='font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;mso-fareast-language:ES;mso-no-proof:\n" +
                "yes'>" + tO.getOfeDescripcion() + "</span></p>\n" +
                "    <p class='MsoNormal' style='text-align:center' align='center'><span style='font-size:16.0pt;line-height:115%;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;\n" +
                "mso-fareast-language:ES;mso-no-proof:yes'>Precio:</span><span style='font-family:\n" +
                "&quot;Arial&quot;,&quot;sans-serif&quot;;mso-fareast-language:ES;mso-no-proof:yes'>\n" +
                "      </span><span style='font-size:18.0pt;line-height:115%;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;\n" +
                "mso-fareast-language:ES;mso-no-proof:yes'>" + tO.getOfePrecio() + " €</span><span style='font-family:\n" +
                "&quot;Arial&quot;,&quot;sans-serif&quot;;mso-fareast-language:ES;mso-no-proof:yes'><span\n" +
                "\n" +
                "          style='mso-spacerun:yes'>&nbsp; </span>-<span style='mso-spacerun:yes'>&nbsp;\n" +
                "          </span></span><span style='font-size:16.0pt;line-height:115%;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;mso-fareast-language:ES;mso-no-proof:yes'>Ahorro:</span><span\n" +
                "\n" +
                "        style='font-family:\n" +
                "&quot;Arial&quot;,&quot;sans-serif&quot;;mso-fareast-language:ES;mso-no-proof:yes'>\n" +
                "      </span><b style='mso-bidi-font-weight:normal'><span style='font-size:24.0pt;line-height:\n" +
                "115%;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:red;mso-fareast-language:ES;mso-no-proof:yes'>" + tO.getOfeAhorro() + "%</span></b><span\n" +
                "\n" +
                "        style='font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;\n" +
                "mso-fareast-language:ES;mso-no-proof:yes'></span></p>\n" +
                "    <p class='MsoNormal' style='text-align:center' align='center'><span style='font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;mso-fareast-language:ES;mso-no-proof:\n" +
                "yes'>Tipo: " + tO.getOfeTipo() + "<br>\n" +
                "      </span><span style='font-size:8.0pt;line-height:115%;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;\n" +
                "mso-fareast-language:ES;mso-no-proof:yes'>" + cadValidez + "</span></p>";
            
            return cadDetalle;
        }
        
    }
